const puppeteer = require('puppeteer');
const mongoose = require('mongoose');
const HtmlUrl = mongoose.model('HtmlUrl')
const Movie = mongoose.model('Movie')
const fs = require('fs');

class OnePaChong {
  constructor() {
    this.num=0;
    this.data = [],

      // 初始化
      this.init()
  }
  // 初始化函数
  async init() {
    console.log('正在启动浏览器...')
    this.browser = await puppeteer.launch();
    console.log('正在打开新页面...')
    try {
      this.page = await this.browser.newPage();
    } catch (error) {
      console.log(error + '报错了')
      this.page.reload();
    }



    this.data = await HtmlUrl.find({})

    for (let i = 0; i < this.data.length; i++) {

      await this.getAll(this.data[i].htmlurl)


    }




    this.closeBrowser()
  }
  async getAll(cut) {

    // 延时 1000 毫秒
    // await timeout(1000);
    const page = this.page
    try {
      await page.goto(cut, {
        waitUntil: 'networkidle2'
      });
    } catch (error) {
      if (error) {
        this.page.reload();
      }
    }

    let movieData;
    // 获取信息
    try {
      let pageData = await page.evaluate(() => {
        var jQuery = window.jQuery
        

        var translatedTitle = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[1].split('名')[1].trim()
        var title = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[2].split('名')[1].trim()
        var age = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[3].split('代')[1].trim()
        var place = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[4].split('地')[1].trim()
        var classification = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[5].split('别')[1].trim()
        var language = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[6].split('言')[1].trim()
        var releaseDate = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[8].split('期')[1].split('(')[0].trim()
        var doubanGrade = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[10].split('分')[1].split('/')[0].trim()
        var lengthOf = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[14].split('长')[1].split('分')[0].trim()
        var direct = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[15].split('演')[1].trim()
        var summary = jQuery('#Zoom span').text().split('【下载地址】')[0].split('◎')[17].split('介')[1].trim()
        var cover = jQuery('#Zoom span img').eq(0).attr('src').trim()
        var poster = jQuery('#Zoom span img').eq(1).attr('src').trim()
        var baidudyun = jQuery('#Zoom span a').eq(0).attr('thunderrestitle').trim()


        return {
          translatedTitle,
          title,
          age,
          place,
          classification,
          language,
          releaseDate,
          doubanGrade,
          lengthOf,
          direct,
          summary,
          cover,
          poster,
          baidudyun
        }
      })
        //console.log(cut)
       // console.log(pageData)
      // console.log('-------------------------------------------- start')

      // console.log('采集状态：', '成功');


      // console.log('-------------------------------------------- end')
      let movie=await Movie.findOne({
        title:pageData.title
      })
     // console.log(movie)
      if(!movie){
        this.num++
      
       let movie= new Movie(pageData)
        await movie.save()
        console.log( this.num++)
      }
      // let typeClass = pageData.classification.split('/')
      // for (let i = 0; i < typeClass.length; i++) {
      //   let item = typeClass[i]
      //   let cat = await Category.findOne({name:item})
      //   if(!cat){
      //     cat=new Category({
      //       name:item
      //     })
      //   }
      // }

    

    } catch (error) {
      //console.log(cut)
      console.log('错误信息：', error)

    }

  }
  // 关闭浏览器
  async closeBrowser() {
    console.log('正在关闭浏览器...')
    await this.browser.close()
  }
}

// 启用爬虫
new OnePaChong()